#include "realdatawidget.h"

#include <QDate>
#include <QSqlQuery>

RealDataWidget::RealDataWidget(QWidget *parent) :
    QWidget(parent)
{
    initui();
}
RealDataWidget::~RealDataWidget()
{
}
void RealDataWidget::initui()
{
    ZigBeeTree = new ZigBeeNodeTreeWidget;
    realDataRight = new RealDataRightWidget;

    //    mainLayout = new QGridLayout;
    //    mainLayout->addWidget(ZigBeeTree,0,0,1,4);
    //    mainLayout->addWidget(realDataRight,0,5,1,4);
    //    setLayout(mainLayout);


    mainLayout = new QHBoxLayout;
    QSizePolicy size;
    QSizePolicy size1;
    size.setHorizontalPolicy(QSizePolicy::Ignored);
    size.setVerticalPolicy(QSizePolicy::Preferred);
    size.setHorizontalStretch(32);
    size.setVerticalStretch(1);
    size1.setHorizontalPolicy(QSizePolicy::Maximum);
    size1.setVerticalPolicy(QSizePolicy::Preferred);
    size1.setHorizontalStretch(10);
    size1.setVerticalStretch(1);
    ZigBeeTree->setSizePolicy(size1);
    realDataRight->setSizePolicy(size);
    mainLayout->addWidget(ZigBeeTree);
    mainLayout->addWidget(realDataRight);
    setLayout(mainLayout);

    connect(ZigBeeTree,SIGNAL(sig_activeNode(QString)),realDataRight,SLOT(slt_showAllorOne(QString)));
    connect(ZigBeeTree,SIGNAL(sig_activeNode(QString)),this,SLOT(slt_setCurrentNode(QString)));
    connect(ZigBeeTree,SIGNAL(sig_activeNode(ushort)),this,SLOT(slt_setCurrentNode(ushort)));
    connect(realDataRight,SIGNAL(sig_aFilter(Filter)),this,SIGNAL(sig_aFilter(Filter)));
    connect(realDataRight,SIGNAL(sig_tree_clear()),this,SLOT(slt_tree_clear()));


}

void RealDataWidget::slt_lineGrap(const QString& adress, int value)//实现分发,画图只能一个节点
{
    if (m_currentNode != "ALLNODE" && m_currentNode == adress) {
        realDataRight->slt_lineGrapShowData(adress,value);
    } else {

    }

}

void RealDataWidget::slt_netTopologyBuild(const QString &value)
{
    realDataRight->slt_netTopologyBuild(value);
}

void RealDataWidget::slt_setCurrentNode(const QString &adress)
{
    m_currentNode = adress;
}
void RealDataWidget::slt_setCurrentNode(ushort name)
{
    m_currentName = name;
}
// headNames<<tr("日期")<<tr("时间")<<tr("节点")<<tr("设备名")<<tr("数值")<<tr("状态");
void RealDataWidget::slt_setListItems(const QString &/*adress*/,int name,int value,int flag)
{
    //    QTableWidgetItem *item1 = new QTableWidgetItem(QDate::currentDate().toString("yyyy-MM-dd"));//日期
    //    QTableWidgetItem *item2 = new QTableWidgetItem(QTime::currentTime().toString("hh:mm:ss"));//时间
    //    QTableWidgetItem *item3 = new QTableWidgetItem(adress);//地址
    //    QTableWidgetItem *item4 = new QTableWidgetItem(QString::number(name));//设备名
    //    QTableWidgetItem *item5 = new QTableWidgetItem(QString::number(value));//数值
    //    QTableWidgetItem *item6 = new QTableWidgetItem(QString::number(flag));
    QString currentDate = QDate::currentDate().toString("yyyy-MM-dd");
    QString currentTime = QTime::currentTime().toString("hh:mm:ss");
    QTableWidgetItem *item1 = new QTableWidgetItem(QString::number(name));//设备名
    QTableWidgetItem *item2 = new QTableWidgetItem(currentDate);//日期
    QTableWidgetItem *item3 = new QTableWidgetItem(currentTime);//时间
    QTableWidgetItem *item4 = new QTableWidgetItem(Node::getFlagInfo(flag));
    QTableWidgetItem *item5 = new QTableWidgetItem(QString::number(value));//数值


    QList<QTableWidgetItem*> list;
    //日期 时间 设备名 描述 值
    list  << item2 << item3 << item1 << item4 << item5 /*<< item6*/;

    realDataRight->setListWidgetItems(list);

    DBcontrol::insertArealData(QStringList() << QString::number(name) << currentDate
                               << currentTime << Node::getFlagInfo(flag)
                               << QString::number(value));
}


//void RealDataWidget::insertSonserDataIntoDB(const QList<QTableWidgetItem *> &list)
//{
//    QSqlQuery query;
//    query.prepare("insert into DATA (_date, _time, _adress, _name, _value, status) "
//                  "values (?, ?, ?, ?, ?, ?)");
//    foreach (const QTableWidgetItem* t, list) {
//        query.addBindValue(t->text());
//    }
//}

void RealDataWidget::slt_newNode(const Node &n)
{
    realDataRight->slt_newNode(n);
    ZigBeeTree->slt_nodeChange(n.getName(),n.getState());
}
//void RealDataWidget::slt_oldNode(const Node &n)
//{
//    realDataRight->slt_oldNode(n);
//}
//void RealDataWidget::slt_nodeChange(ushort name, bool states)
//{
//    //更新树形图
//    ZigBeeTree->slt_nodeChange(name,states);
//    //更新拓扑图
//    // realDataRight->slt_statesChange(name,states);
//}

void RealDataWidget::slt_ChangNodeState(const Node &n)
{

    realDataRight->slt_ChangNodeState(n);
    if (n.getDescription() == Coordinator) ZigBeeTree->clear();
    ZigBeeTree->slt_nodeChange(n.getName(),n.getState());

}

void RealDataWidget::slt_ChangNodeState(int type,const QString &addr,
                                        const QString &paddr,
                                        ushort _name,bool status)
{

    realDataRight->slt_ChangNodeState(type,addr,paddr,_name,status);
    if (type == Coordinator) ZigBeeTree->clear();
    ZigBeeTree->slt_nodeChange(_name,status);

}

void RealDataWidget::slt_lineGrap(ushort name, int value)
{
    /**
     *过滤一些选项
     *
     *
     */
    if (m_currentNode != "ALLNODE" && m_currentName == name) {
        realDataRight->slt_lineGrapShowData(name,value);
    } else {

    }


}

void RealDataWidget::slt_updataNodeValue(ushort name, int value)
{
    realDataRight->slt_updataNodeValue(name,value);
}


void RealDataWidget::slt_tree_clear()
{
    ZigBeeTree->clear();
}
